<template>
    <div class="reader-login">
        <el-col :span="6" :offset="9" style="padding-top: 10px">
            <el-form  :model="ruleForm" label-width="120px" class="edit" :rules="rules" ref="ruleForm">
                <el-form-item label="姓名:" prop="username">
                    <el-input v-model="ruleForm.username" placeholder="请输入读者姓名" ></el-input>
                </el-form-item>
                <el-form-item label="学号:" prop="number">
                    <el-input v-model="ruleForm.number" placeholder="请输入学号" ></el-input>
                </el-form-item>
                <el-form-item label="密码:" prop="password">
                    <el-input v-model="ruleForm.password" placeholder="请输入读者名称" ></el-input>
                </el-form-item>
                <el-form-item label="重复密码:" prop="checkPass">
                    <el-input v-model="ruleForm.checkPass" placeholder="请输入读者名称" ></el-input>
                </el-form-item>
                <el-form-item label="性别:" prop="sex">
                    <el-input v-model="ruleForm.sex" placeholder="请输入性别" ></el-input>
                </el-form-item>
                <el-form-item label="年龄:" prop="age">
                    <el-input v-model="ruleForm.age" placeholder="请输入年龄" ></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="onSubmit">保存</el-button>
                    <el-button @click="onCancel">取消</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </div>
</template>

<script>
    import  { create } from '../../../module/reader/reader/readerInfo';
    export default {
        data () {
            let validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            let validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    username: '',
                    password:'',
                    checkPass: '',
                    sex: '',
                    age: '',
                    number: '',
                },
                rules: {
                    username: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                    ],
                    number: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                    ],
                    password:  [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    sex: [
                        {required: true, message: '请输入性别', trigger: 'blur'},
                    ],
                    age: [
                        {required: true, message: '请输入年龄', trigger: 'blur'},
                    ],
                },
                checked: true
            }
        },
        methods: {
            onSubmit() {
                let _this = this;
                create(this.ruleForm).then(function (data) {
                    _this.$message({
                        type: 'success',
                        center: true,
                        message: '注册成功!'
                    });
                    console.log(data)
                 /*   sessionStorage.setItem('readerName', result.data.username);
                    sessionStorage.setItem('readerId', result.data.id);
                    this.$router.push({path: '/reader/book/book-entry'});*/
                }).catch(function (error) {
                    _this.$message({
                        type: 'error',
                        center: true,
                        message: '注册失败!'
                    });
                });
                this.$router.push({
                    name: "reader-login"
                })
            },
            onCancel() {
                this.$router.push({
                    name: "reader-book-entry"
                })
            }
        }
    }
</script>

<style>
    .reader-login {
        padding-top: 100px;
        text-align:center;
        height: 2000px;
    }
</style>
